package com.example.fangyimanagement;

import com.example.fangyimanagement.core.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/orgManage")
public class OrgManageController {
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @RequestMapping("/addOrg")
    public Result<Void> addOrg(@RequestBody Map<String, String> param) {
        String orgCode = param.get("orgCode");
        String orgName = param.get("orgName");
        String remark = param.get("remark");
        // 先查询库里有没有这个组织编码，只有编码要做防重复的处理
        List<Map<String, Object>> count = this.jdbcTemplate.queryForList("SELECT * FROM glpt_org WHERE org_code=?", orgCode);
        if (!count.isEmpty()) {
            return Result.failed("和其他组织的编码重复，请修改~");
        }
        //        操作数据库
        String sql = "insert into glpt_org (org_code, org_name, create_time, remark) " +
                "values (?, ?, CURRENT_TIME, ?)";
        int rows = this.jdbcTemplate.update(sql, orgCode, orgName, remark);
        if (rows > 0) {
            return Result.success();
        } else {
            return Result.failed("添加失败");
        }
    }
    @RequestMapping("/deleteOrg")
    public Result<Void> deleteOrg(@RequestBody Map<String, String> param) {
        String id = param.get("id");
        List<Map<String, Object>> orgList = this.jdbcTemplate.queryForList("select * from glpt_org where id=?", id);
        if (!orgList.isEmpty() && orgList.get(0).get("org_code") != null) {
            List<Map<String, Object>> useThisOrgUserList = this.jdbcTemplate.queryForList(
                    "select * from glpt_user where org_code=?", orgList.get(0).get("org_code"));
            if (!useThisOrgUserList.isEmpty()) {
                return Result.failed("有用户正在使用此角色，请先为该用户授予其他角色");
            } else {
                //        操作数据库
                int rows = this.jdbcTemplate.update("delete from glpt_org where id =?", id);
                if (rows > 0) {
                    return Result.success();
                } else {
                    return Result.failed("删除角色失败");
                }
            }
        } else {
            return Result.failed("没找到这个角色");
        }
    }
    @RequestMapping("/updateOrg")
    public Result<Void> updateOrg(@RequestBody Map<String, String> param) {
        String id = param.get("id");
        String orgCode = param.get("orgCode");
        String orgName = param.get("orgName");
        String remark = param.get("remark");
        // 先查询库里有没有这个组织编码，只有编码要做防重复的处理
        List<Map<String, Object>> count = this.jdbcTemplate.queryForList("SELECT * FROM glpt_org WHERE org_code=? AND id !=?", orgCode, id);
        if (!count.isEmpty()) {
            return Result.failed("和其他组织的编码重复，请修改~");
        }
        //        操作数据库
        String sql = "update glpt_org set org_code=?, org_name=?, remark=? where id =?";
        int rows = this.jdbcTemplate.update(sql, orgCode, orgName, remark, id);
        if (rows > 0) {
            return Result.success();
        } else {
            return Result.failed("添加失败");
        }
    }
    @RequestMapping("/orgList")
    public Result<List<Map<String, Object>>> orgList(@RequestBody Map<String, String> param) {
        int page = Integer.parseInt(param.get("page"));
        int size = Integer.parseInt(param.get("size"));
        String orgName = param.get("orgName");
        List<Object> paramList = new ArrayList<>();

        String sql = "SELECT id, org_code AS orgCode, org_name AS orgName, parent_org_code AS parentOrgCode, create_time as createTime, remark FROM glpt_org";
        if (orgName != null && !orgName.isEmpty()) {
            sql = sql + " WHERE org_name like '%" + orgName + "%'";
        }

        String countSql = "SELECT COUNT(1) FROM ("+ sql +") t";
        Integer count = this.jdbcTemplate.queryForObject(countSql, Integer.class, paramList.toArray());

        sql = sql + " ORDER BY id LIMIT ?, ?";
        paramList.add((page - 1) * size);
        paramList.add(size);
        List<Map<String, Object>> orgList = this.jdbcTemplate.queryForList(sql, paramList.toArray());
        return Result.success(orgList).setTotal(count);
    }
    @RequestMapping("/allOrgList")
    public Result<List<Map<String, Object>>> allOrgList(@RequestBody Map<String, String> param) {
        List<Map<String, Object>> allOrgList = this.jdbcTemplate.queryForList("SELECT org_code AS code, org_name AS name FROM glpt_org");
        return Result.success(allOrgList);
    }
}
